<%@ page import="java.sql.*, javax.sql.*, javax.naming.*" %>
<%@page import="support.*, java.util.*" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
<!-- Three column style settings -->
#left {
  margin-right: 153px;
}
#right {
  margin-left: 150px;
}

#left_wrapper {
  width: 50%;
  margin-right: -150px;
  float: left;
  position: relative;
  z-index: 0;
}

#middle_wrapper {
  width: 300px;
  margin-right: -150px;
  float: left;
  position: relative;
  z-index: 2;
}

#right_wrapper {
  width: 49.9%;
  float: left;
  position: relative;
  z-index: 1;
}
</style>
<title>Country Page</title>
</head>
<body>
    <!-- Gets names from previous page (names.html) -->
    <% 
    // gets first name
    String first = request.getParameter("first");
    session.setAttribute("first", first); 
    // gets last name
    String last= request.getParameter("last");
    session.setAttribute("last", last); 
    // gets middle name
    String middle= request.getParameter("middle");
    session.setAttribute("middle", middle); 
    %>
    
  <%
   //Creating linkedHashSet to be used in this session later 
   LinkedHashSet degrees = new LinkedHashSet();
   session.setAttribute("degrees", degrees);
   %>
    
    <!-- User Prompts for country of citizenship -->
    Welcome, <%= first + " " + middle + " " + last %>! <p>
    What is your country of citizenship? <br>
    Select below: <br> <br>
    <!--  Hidden country object to keep track of choice-->
    <input type="hidden" name="citizen" value="">
    
    <%
    
    // Connect to ConnectionPool
    Connection conn = null;
    try{
        Context initCtx = new InitialContext();
        DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/CS135");
        conn = ds.getConnection();
    }finally{
    }
    Statement stmt = conn.createStatement();
    %>
    
    <% 
    // use sql statement to display all the specializations
    ResultSet rset = stmt.executeQuery("SELECT COUNT(*) FROM countries");
    int counter = 0;  // keeps track of the country we are on
    int countrySize = 0;  // total size of the country table
    if(rset.next()){
        countrySize = rset.getInt(1);
    }
    // use sql statement to display all the countries
    rset = stmt.executeQuery("SELECT * FROM countries");
    %>        
    
    
    <!-- Create the LEFT COLUMN -->
    <div id="left_wrapper">
    <div id="left">
    <%
     // loops through first 1/3 of the countries
     for(int i=0; i<countrySize/3; i++, counter++){
       // use hidden object to make href
       if(rset.next()){
         out.print(
               "<a href=\"residence.jsp?citizen=" + rset.getString(1)
               + "\">" + rset.getString(1) + "</a>" + "<br>"); 
       }
     }
    %>
    </div>
    </div>
    
     <!-- Create the MIDDLE COLUMN -->
    <div id="middle_wrapper">
    <div id="middle">
    <%
    // loops through second 1/3 of the countries
     for(int i =0;i<countrySize/3; i++, counter++){
         // use hidden object to make href
         if(rset.next()){
             out.print(
                   "<a href=\"residence.jsp?citizen=" + rset.getString(1)
                   + "\">" + rset.getString(1) + "</a>" + "<br>"); 
           }
     }
    %>
    </div>
    </div>
    
     <!-- Create the RIGHT COLUMN -->
    <div id="right_wrapper">
    <div id="right">
    <%
    // loops through rest of the countries
     for(; counter<countrySize; counter++){
         // use hidden object to make href
         if(rset.next()){
             out.print(
                   "<a href=\"residence.jsp?citizen=" + rset.getString(1)
                   + "\">" + rset.getString(1) + "</a>" + "<br>"); 
           }     
      }
    %>
    </div>
    </div>
    
</body>
</html>